//$ is not defined
// 后台设置cors后 前端一般会发送两次请求 options:探测请求 1.是否跨域  2.正常的请求
// {token:'123} // key  value
// ?token=123

const changeParams = (obj) => Object.keys(obj)
  .map((item, index) => index === 0 ?
    `?${item}=${obj[item]}` :
    `&${item}=${obj[item]}`).join('');

const BASE_URL = `http://localhost:3000`;

console.log(changeParams({token: 123, msg: 123}));

function requestGET(url, params) {
  let str = changeParams(params);
  return new Promise((resolve, reject) => {
    $.ajax({
      url: `${BASE_URL}${url}${str}`,
      type: "get",
      dataType: "json",//数据类型
      success: function (res) {
        resolve(res);
      },
      error(err) {
        reject(err);
      }
    });
  })
}

function* generatorRequest() {
  let res1 = yield requestGET('/login', {});
  let res2 = yield requestGET('/list', {token: res1.token});
  let {users, code} = res2;
  let arrUserId = users.map(item => item.id);
  let idIndex = Math.floor(Math.random() * arrUserId.length);
  console.log(idIndex);
  let res3 = yield requestGET('/item', {id: arrUserId[idIndex]});
  console.log("=============华丽丽的分割线=============");
  console.log(res1, res2, res3);
}

run(generatorRequest);


//
// function Login() {
//   return new Promise(resolve => {
//     $.ajax({
//       url: "http://localhost:3000/login",
//       type: "get",
//       dataType: "json",//数据类型
//       success: function (msg) {
//         resolve(msg);
//       }
//     });
//   });
// }

//run(generator);
